//
//  SYShareBanJiListView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/2/27.
//  Copyright © 2020年 SanYi. All rights reserved.
//

#import "SYShareBanJiListView.h"
#import "SYTopLabView.h"
#import "SYShareCliassTableViewCell.h"
#import "ZXFDCSelectedView.h"
#import "DatePickerView.h"


@interface SYShareBanJiListView ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SYBackTopView *backView;//顶部viwe
@property (strong, nonatomic) SYTopLabView *topLabView;
@property (strong, nonatomic) UITableView *curTableView;//
@property (strong, nonatomic) NSMutableArray *datalistArr;
@property (assign, nonatomic) NSInteger index;;
@property (copy, nonatomic) NSString *packageId;//选择资源包id
@property (strong, nonatomic) UIView *contView;//功能view
@property (strong, nonatomic) UIButton *nextBtn;//下一步btn
//@property (strong, nonatomic) SYJiaoCaiCell ;
/**
 开始时间
 */
@property (nonatomic, strong) UILabel *jieZhiRiQiLabel;
@property (nonatomic, strong) ZXFDCSelectedView *kaiShiTimeView;
/**
 开始时间
 */
@property (nonatomic, strong) UILabel *huishouRiQiLabel;
@property (nonatomic, strong) ZXFDCSelectedView *huishouTimeView;

//groupId    是    string    集团ID
//campusId    是    string    分校DI
//resourceGroupId    是    string    组ID
//resourcePackageId    是    string    教材包ID
//classId    是    string    班级ID
//grandDate    是    string    发送日期
//recycleDate    是    string    回收日期
@property (nonatomic, copy) NSString *groupId;
@property (nonatomic, copy) NSString *campusId;
@property (nonatomic, copy) NSString *resourceGroupId;
@property (nonatomic, copy) NSString *resourcePackageId;
@property (nonatomic, copy) NSString *classId;
@property (nonatomic, copy) NSString *grandDate;
@property (nonatomic, copy) NSString *recycleDate;
@end
@implementation SYShareBanJiListView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}
- (void)initSubView {
    self.index = -1;
    [self addSubview:self.backView];
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    [self addSubview:self.topLabView];
    [self.topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.backView.mas_bottom);
        make.height.mas_equalTo(45);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.topLabView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom).offset(-300);
    }];
    
    _datalistArr = [NSMutableArray array];
    
    [_curTableView registerCellWithNIbNames:@[@"SYShareCliassTableViewCell"]];
    [self addSubview:self.contView];
    [self.contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.curTableView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
    }];
    //下发资源包
    [self.contView addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contView);
        make.bottom.equalTo(self.contView).offset(-50);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(60);
    }];
    
    
    [self.contView addSubview:self.jieZhiRiQiLabel];
    [_jieZhiRiQiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contView.mas_left).offset(50);
        make.top.equalTo(self.contView.mas_top).offset(80);
        make.height.equalTo(@30);
    }];
    //选择开始日期
    [self.contView addSubview:self.kaiShiTimeView];
    [self.kaiShiTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.jieZhiRiQiLabel.mas_centerY);
        make.left.equalTo(self.jieZhiRiQiLabel.mas_right).offset(10);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(45);
    }];
    
    [self.contView addSubview:self.huishouRiQiLabel];
    [self.huishouRiQiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.kaiShiTimeView.mas_centerY);
        make.left.equalTo(self.kaiShiTimeView.mas_right).offset(100);
        make.height.mas_equalTo(30);
    }];
    
    [self.contView addSubview:self.huishouTimeView];
    [self.huishouTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.huishouRiQiLabel.mas_centerY);
        make.left.equalTo(self.huishouRiQiLabel.mas_right).offset(10);
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(45);
    }];
    
}
- (void)requestDataGroupId:(NSString *)groupId packageId:(NSString *)packageId {
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI zy_requestGetGroupPackageClassListGroupId:groupId packageId:packageId success:^(NSArray *arr) {
        [MBProgressHUD mj_hideHUD];
        [self.datalistArr addObjectsFromArray:arr];
        [self.curTableView reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
    }];
    
}



- (void)requestDataGroupId:(NSString *)groupId {
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI zy_requestGetResourceEmployeeListPackageId:groupId success:^(NSArray *arr) {
        [MBProgressHUD mj_hideHUD];
        [self.datalistArr addObjectsFromArray:arr];
        [self.curTableView reloadData];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        
    }];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return _datalistArr.count;
//    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYShareCliassTableViewCell *shareCell = [tableView dequeueReusableCellWithIdentifier:@"SYShareCliassTableViewCell" forIndexPath:indexPath];
    shareCell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = self.datalistArr[indexPath.row];
    shareCell.fenxiaoLab.text = [dict nilObjectForKey:@"campusName"];
    shareCell.banjiLab.text = [dict nilObjectForKey:@"className"];
    
    NSString *grandDate = [dict nilObjectForKey:@"grandDate"];
    NSArray *grandArr = [grandDate componentsSeparatedByString:@" "];
    
    shareCell.fasongriqiLab.text = grandArr.firstObject;
    
    NSString *recycleDate = [dict nilObjectForKey:@"recycleDate"];
    NSArray *recycleDateArr = [recycleDate componentsSeparatedByString:@" "];
    
    shareCell.huishouriLab.text = recycleDateArr.firstObject;
    shareCell.chuangjianrenLab.text = [dict nilObjectForKey:@"createUser"];
    NSMutableString *isSend = [dict nilObjectForKey:@"isSend"];
    shareCell.shiofuyifenxiangLab.text = [isSend integerValue] == 0?@"未发送":@"已发送";
    if ([isSend integerValue]== 1) {
        shareCell.fenxiangBtn.hidden = YES;
    } else {
        shareCell.fenxiangBtn.hidden = NO;
        __weak typeof(self) weakSelf = self;
        shareCell.block = ^(NSInteger index) {
            weakSelf.index = indexPath.row;
            [weakSelf.curTableView reloadData];
            
            weakSelf.groupId = [dict nilObjectForKey:@"groupId"];
            weakSelf.campusId = [dict nilObjectForKey:@"campusId"];
            weakSelf.classId = [dict nilObjectForKey:@"classId"];
            weakSelf.resourceGroupId = [dict nilObjectForKey:@"resourceGroupId"];
            weakSelf.resourcePackageId = [dict nilObjectForKey:@"resourcePackageId"];
            
        };
    }
    
    if (self.index == indexPath.row) {
        [shareCell.fenxiangBtn setImage:[UIImage imageNamed:@"banji_select"] forState:UIControlStateNormal];
    } else {
        [shareCell.fenxiangBtn setImage:[UIImage imageNamed:@"banji_no_select"] forState:UIControlStateNormal];
    }
    return shareCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    NSDictionary *dict = self.datalistArr[indexPath.row];
    //    NSString *path = [dict nilObjectForKey:@"path"];//观看地址
    //    NSString *type = [dict nilObjectForKey:@"type"];//类型
    
}
//头部
- (SYBackTopView *)backView {
    if (!_backView) {
        _backView = [[SYBackTopView alloc] initWithTitleName:@"发送班级列表" baseView:self];
    }
    return _backView;
}
#pragma mark -
- (SYTopLabView *)topLabView {
    if (!_topLabView) {
        NSArray *xiaoArr = nil;
        CGFloat bili;
        
        xiaoArr = @[@"分校名称",@"班级名称",@"发送日期",@"回收日期",@"创建人",@"是否已发送",@"是否分享"];
        bili = 0.143;
        
        
        _topLabView = [[SYTopLabView alloc] initWithDataNameArr:xiaoArr width:bili];
    }
    return _topLabView;
}

- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] init];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.tableFooterView = [UIView new];
        
    }
    return _curTableView;
}
- (UIView *)contView {
    if (!_contView) {
        _contView = [[UIView alloc] init];
        _contView.backgroundColor = [UIColor whiteColor];
    }
    return _contView;
}
//下一步but
- (UIButton *)nextBtn {
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _nextBtn.layer.borderWidth = 1;
        _nextBtn.layer.borderColor = [UIColor colorWithHexString:@"#20B097"].CGColor;
        _nextBtn.layer.cornerRadius = 5;
        _nextBtn.layer.masksToBounds = YES;
        [_nextBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_nextBtn setTitle:@"选择分享班级" forState:UIControlStateNormal];
        //        [_nextBtn setTintColor:[UIColor colorWithHexString:@"#20B097"]];
        [_nextBtn setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateNormal];
        _nextBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    return _nextBtn;
}

- (void)nextBtnClick {
 
    
    if (self.resourceGroupId.length == 0) {
        [MBProgressHUD zr_showError:@"请选择分享班级"];
        return;
    }
    
    if (self.grandDate.length == 0) {
        [MBProgressHUD zr_showError:@"请选择发送日期"];
        return;
    }
    
    if (self.recycleDate.length == 0) {
        [MBProgressHUD zr_showError:@"请选择回收日期"];
        return;
    }
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI zy_requestAddGroupPackageClassGroupid:self.groupId campusid:self.campusId resourceGroupId:self.resourceGroupId resourcePackageId:self.resourcePackageId classId:self.classId grandDate:self.grandDate recycleDate:self.recycleDate success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:@"分享成功"];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:error.domain];
    }];
    
}



//截止日期
- (UILabel *)jieZhiRiQiLabel{
    if (!_jieZhiRiQiLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text  = @"发送日期:";
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = TITLECOLOR;
        label.textAlignment = 2;
        _jieZhiRiQiLabel = label;
    }
    return _jieZhiRiQiLabel;
}
- (ZXFDCSelectedView *)kaiShiTimeView{
    if (!_kaiShiTimeView) {
        ZXFDCSelectedView *view = [[ZXFDCSelectedView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        view.layer.cornerRadius = 2;
        view.layer.borderColor = [UIColorChange colorwithHexString:@"#999999"].CGColor;
        view.layer.borderWidth = 1;
        view.nameLabel.text = @"发送日期";
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedKaiShiTimeView)];
        [view addGestureRecognizer:tap];
        _kaiShiTimeView = view;
    }
    return _kaiShiTimeView;
}
- (void)selectedKaiShiTimeView{
    
        [DatePickerView setDatePickerViewWithFrame:self.frame andTitle:@"发送日期" andSuccessBlock:^(NSString * _Nonnull dateString) {
           
    
            self->_kaiShiTimeView.nameLabel.text = dateString;
            self->_grandDate = dateString;
        }];
}
//回收
- (void)selectedhuishouTimeView {
    [DatePickerView setDatePickerViewWithFrame:self.frame andTitle:@"回收日期" andSuccessBlock:^(NSString * _Nonnull dateString) {
        
        
        self->_huishouTimeView.nameLabel.text = dateString;
        self->_recycleDate = dateString;
    }];
}

//截止日期
- (UILabel *)huishouRiQiLabel{
    if (!_huishouRiQiLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text  = @"回收日期:";
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = TITLECOLOR;
        label.textAlignment = 2;
        _huishouRiQiLabel = label;
    }
    return _huishouRiQiLabel;
}
- (ZXFDCSelectedView *)huishouTimeView{
    if (!_huishouTimeView) {
        ZXFDCSelectedView *view = [[ZXFDCSelectedView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        view.layer.cornerRadius = 2;
        view.layer.borderColor = [UIColorChange colorwithHexString:@"#999999"].CGColor;
        view.layer.borderWidth = 1;
        view.nameLabel.text = @"回收日期";
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedhuishouTimeView)];
        [view addGestureRecognizer:tap];
        _huishouTimeView = view;
    }
    return _huishouTimeView;
}


@end
